﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUSQLDV;
using DTOQLDV;

namespace CTLQLDV
{
    public partial class CTLDuLieu : DevExpress.XtraEditors.XtraUserControl
    {
        public CTLDuLieu()
        {
            _duLieu = new DTODuLieu();
            InitializeComponent();
            KhoiTao();            
        }        
        private void LamTuoi()
        {
            thTenDichVu.EditValue = _duLieu.MaDichVu;           
            thTenDiaDiem.EditValue = _duLieu.MaDiaDiem;          
            thTenDuong.EditValue = _duLieu.MaDuong;           
            thTenPhuong.EditValue = _duLieu.MaPhuong;         
            thTenQuanHuyen.EditValue = _duLieu.MaQuanHuyen;         
            thTenTinhThanh.EditValue = _duLieu.MaTinhThanh;           
            thSoNha.Text = _duLieu.SoNha;
            thKinhDo.Text = _duLieu.KinhDo;
            thViDo.Text = _duLieu.ViDo;
            thChuThich.Text = _duLieu.ChuThich;
            this.Refresh();
        }
        private DTODuLieu _duLieu;
        private void KhoiTao()
        {
            thTenDichVu.Properties.DisplayMember = "TenDichVu";
            thTenDichVu.Properties.ValueMember = "MaDichVu";
            thTenDichVu.Properties.NullText = "Xin chọn tên dịch vụ";
            thTenDichVu.Properties.ActionButtonIndex = 3;
            thTenDichVu.Properties.Buttons[2].Enabled = false;
            thTenDichVu.Properties.Buttons[1].Enabled = false;
            thTenDichVu.EditValue = null;

            thTenDuong.Properties.DisplayMember = "TenDuong";
            thTenDuong.Properties.ValueMember = "MaDuong";
            thTenDuong.Properties.NullText = "Xin chọn tên đường";
            thTenDuong.Properties.ActionButtonIndex = 3;
            thTenDuong.Properties.Buttons[2].Enabled = false;
            thTenDuong.Properties.Buttons[1].Enabled = false;
            thTenDuong.EditValue = null;

            thTenPhuong.Properties.DisplayMember = "TenPhuong";
            thTenPhuong.Properties.ValueMember = "MaPhuong";
            thTenPhuong.Properties.NullText = "Xin chọn tên phường";
            thTenPhuong.Properties.ActionButtonIndex = 3;
            thTenPhuong.Properties.Buttons[2].Enabled = false;
            thTenPhuong.Properties.Buttons[1].Enabled = false;
            thTenPhuong.EditValue = null;

            thTenQuanHuyen.Properties.DisplayMember = "TenQuanHuyen";
            thTenQuanHuyen.Properties.ValueMember = "MaQuanHuyen";
            thTenQuanHuyen.Properties.NullText = "Xin chọn quận huyện";
            thTenQuanHuyen.Properties.ActionButtonIndex = 3;
            thTenQuanHuyen.Properties.Buttons[2].Enabled = false;
            thTenQuanHuyen.Properties.Buttons[1].Enabled = false;
            thTenQuanHuyen.EditValue = null;

            thTenTinhThanh.Properties.DisplayMember = "TenTinhThanh";
            thTenTinhThanh.Properties.ValueMember = "MaTinhThanh";
            thTenTinhThanh.Properties.NullText = "Xin chọn tỉnh thành";
            thTenTinhThanh.Properties.ActionButtonIndex = 3;
            thTenTinhThanh.Properties.Buttons[2].Enabled = false;
            thTenTinhThanh.Properties.Buttons[1].Enabled = false;
            thTenTinhThanh.EditValue = null;

            thTenDiaDiem.Properties.DisplayMember = "TenDiaDiem";
            thTenDiaDiem.Properties.ValueMember = "MaDiaDiem";
            thTenDiaDiem.Properties.NullText = "Xin chọn địa điểm";
            thTenDiaDiem.Properties.ActionButtonIndex = 3;
            thTenDiaDiem.Properties.Buttons[2].Enabled = false;
            thTenDiaDiem.Properties.Buttons[1].Enabled = false;
            thTenDiaDiem.EditValue = null;

            thSoNha.Properties.NullText = "Xin nhập số nhà";
            thSoNha.EditValue = null;

            thKinhDo.Properties.NullText = "Xin nhập kinh độ";
            thKinhDo.EditValue = null;

            thViDo.Properties.NullText = "Xin nhập vĩ độ";
            thViDo.EditValue = null;

            thChuThich.Properties.NullText = "Xin nhập chú thích";
            thChuThich.EditValue = null;

            thTenDichVu.Properties.DataSource = SESSION._dsDichVu;

            thTenDuong.Properties.DataSource = SESSION._dsDuong;

            thTenPhuong.Properties.DataSource = SESSION._dsPhuong;

            thTenQuanHuyen.Properties.DataSource = SESSION._dsQuanHuyen;

            thTenTinhThanh.Properties.DataSource = SESSION._dsTinhThanh;

            thTenDiaDiem.Properties.DataSource = SESSION._dsDiaDiem;
        }
        public DTODuLieu LayDuLieu()
        {
            _duLieu.MaDichVu = int.Parse("0" + thTenDichVu.EditValue);
            _duLieu.MaDuong = int.Parse("0" + thTenDuong.EditValue);
            _duLieu.MaPhuong = int.Parse("0" + thTenPhuong.EditValue);
            _duLieu.MaQuanHuyen = int.Parse("0" + thTenQuanHuyen.EditValue);
            _duLieu.MaTinhThanh = int.Parse("0" + thTenTinhThanh.EditValue);
            _duLieu.MaDiaDiem = int.Parse("0" + thTenDiaDiem.EditValue);
            _duLieu.SoNha = thSoNha.EditValue + "";
            _duLieu.ChuThich = thChuThich.EditValue + "";
            _duLieu.ViDo = thViDo.EditValue + "";

            return _duLieu;
        }
        public void GanDuLieu(DTODuLieu dtoDL)
        {
            _duLieu = dtoDL;
            LamTuoi();
        }
        private void HienThiCapNhat(LookUpEdit lk)
        {
            lk.Properties.Buttons[0].Enabled = false;
            lk.Properties.Buttons[1].Enabled = true;            
            lk.Properties.Buttons[2].Enabled = true;
        }
        private void ThoatXuLi(LookUpEdit lk, int ma)
        {
            lk.Properties.Buttons[0].Enabled = true;
            lk.Properties.Buttons[1].Enabled = false;
            lk.Properties.Buttons[1].Tag = null;
            lk.Properties.Buttons[0].Tag = null;
            lk.Properties.Buttons[2].Enabled = false;
            lk.EditValue = ma;
        }
        private void HienThiThemMoi(LookUpEdit lk)
        {
            lk.Properties.Buttons[0].Enabled = true;
            lk.Properties.Buttons[1].Enabled = false;           
            lk.Properties.Buttons[2].Enabled = true;
        }        
        private void thTenDiaDiem_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            switch (e.Button.Index)
            {
                case 0:
                    {
                        if (thTenDiaDiem.Properties.Buttons[0].Tag != null)
                        {
                            DTODiaDiem dto = new DTODiaDiem();
                            dto.TenDiaDiem = thTenDiaDiem.Text+"";
                            BUSDiaDiem.ThemDiaDiem(dto);
                            SESSION._dsDiaDiem.Add(dto);                            
                            ThoatXuLi(thTenDiaDiem,dto.MaDiaDiem);                            
                        }
                        else
                        {
                            HienThiThemMoi(thTenDiaDiem);
                            thTenDiaDiem.Properties.Buttons[0].Tag = 1;                           
                        }
                    } break;
                case 1:
                    {
                        if (thTenDiaDiem.Properties.Buttons[1].Tag != null)
                        {
                            DTODiaDiem dto = (DTODiaDiem)thTenDiaDiem.Properties.Buttons[1].Tag;
                            dto.TenDiaDiem = thTenDiaDiem.Text;
                            BUSDiaDiem.CapNhatDiaDiem(dto);
                            ThoatXuLi(thTenDiaDiem,dto.MaDiaDiem);
                            thTenDiaDiem.EditValue = dto.MaDiaDiem;
                        }
                        else
                        {
                            thTenDiaDiem.Properties.Buttons[1].Tag = thTenDiaDiem.GetSelectedDataRow();
                            HienThiCapNhat(thTenDiaDiem);
                        }
                    } break;               
                default: ThoatXuLi(thTenDiaDiem,_duLieu.MaDiaDiem); break;
            }  
        }

        private void thTenDiaDiem_EditValueChanged(object sender, EventArgs e)
        {
            if (thTenDiaDiem.ItemIndex >= 0 && thTenDiaDiem.Properties.Buttons[0].Tag==null)
            {
                thTenDiaDiem.Properties.Buttons[1].Enabled = true;
            }
            else
            {
                thTenDiaDiem.Properties.Buttons[1].Enabled = false;
            }
        }

        private void thTenDichVu_EditValueChanged(object sender, EventArgs e)
        {
            if (thTenDichVu.ItemIndex >= 0 && thTenDichVu.Properties.Buttons[0].Tag == null)
            {
                thTenDichVu.Properties.Buttons[1].Enabled = true;
            }
            else
            {
                thTenDichVu.Properties.Buttons[1].Enabled = false;
            }
        }

        private void thTenDuong_EditValueChanged(object sender, EventArgs e)
        {
            if (thTenDuong.ItemIndex >= 0 && thTenDuong.Properties.Buttons[0].Tag == null)
            {
                thTenDuong.Properties.Buttons[1].Enabled = true;
            }
            else
            {
                thTenDuong.Properties.Buttons[1].Enabled = false;
            }
        }

        private void thTenPhuong_EditValueChanged(object sender, EventArgs e)
        {
            if (thTenPhuong.ItemIndex >= 0 && thTenPhuong.Properties.Buttons[0].Tag == null)
            {
                thTenPhuong.Properties.Buttons[1].Enabled = true;
            }
            else
            {
                thTenPhuong.Properties.Buttons[1].Enabled = false;
            }
        }

        private void thTenQuanHuyen_EditValueChanged(object sender, EventArgs e)
        {
            if (thTenQuanHuyen.ItemIndex >= 0 && thTenQuanHuyen.Properties.Buttons[0].Tag == null)
            {
                thTenQuanHuyen.Properties.Buttons[1].Enabled = true;
            }
            else
            {
                thTenQuanHuyen.Properties.Buttons[1].Enabled = false;
            }
        }

        private void thTenTinhThanh_EditValueChanged(object sender, EventArgs e)
        {
            if (thTenTinhThanh.ItemIndex >= 0 && thTenTinhThanh.Properties.Buttons[0].Tag == null)
            {
                thTenTinhThanh.Properties.Buttons[1].Enabled = true;
            }
            else
            {
                thTenTinhThanh.Properties.Buttons[1].Enabled = false;
            }
        }

        private void thTenDichVu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            switch (e.Button.Index)
            {
                case 0:
                    {
                        if (thTenDichVu.Properties.Buttons[0].Tag != null)
                        {
                            DTODichVu dto = new DTODichVu();
                            dto.TenDichVu = thTenDichVu.Text + "";
                            BUSDichVu.ThemDichVu(dto);
                            SESSION._dsDichVu.Add(dto);
                            ThoatXuLi(thTenDichVu,dto.MaDichVu);
                            
                        }
                        else
                        {
                            HienThiThemMoi(thTenDichVu);
                            thTenDichVu.Properties.Buttons[0].Tag = 1;
                        }
                    } break;
                case 1:
                    {
                        if (thTenDichVu.Properties.Buttons[1].Tag != null)
                        {
                            DTODichVu dto = (DTODichVu)thTenDichVu.Properties.Buttons[1].Tag;
                            dto.TenDichVu = thTenDichVu.Text;
                            BUSDichVu.CapNhatDichVu(dto);
                            ThoatXuLi(thTenDichVu,dto.MaDichVu);
                        }
                        else
                        {
                            thTenDichVu.Properties.Buttons[1].Tag = thTenDichVu.GetSelectedDataRow();
                            HienThiCapNhat(thTenDichVu);
                        }
                    } break;
                default: ThoatXuLi(thTenDichVu,_duLieu.MaDichVu); break;
            }  
        }

        private void thTenDuong_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            switch (e.Button.Index)
            {
                case 0:
                    {
                        if (thTenDuong.Properties.Buttons[0].Tag != null)
                        {
                            DTODuong dto = new DTODuong();
                            dto.TenDuong = thTenDuong.Text + "";
                            BUSDuong.ThemDuong(dto);
                            SESSION._dsDuong.Add(dto);
                            ThoatXuLi(thTenDuong,dto.MaDuong);
                        }
                        else
                        {
                            HienThiThemMoi(thTenDuong);
                            thTenDuong.Properties.Buttons[0].Tag = 1;
                        }
                    } break;
                case 1:
                    {
                        if (thTenDuong.Properties.Buttons[1].Tag != null)
                        {
                            DTODuong dto = (DTODuong)thTenDuong.Properties.Buttons[1].Tag;
                            dto.TenDuong = thTenDuong.Text;
                            BUSDuong.CapNhatDuong(dto);
                            ThoatXuLi(thTenDuong,dto.MaDuong);
                        }
                        else
                        {
                            thTenDuong.Properties.Buttons[1].Tag = thTenDuong.GetSelectedDataRow();
                            HienThiCapNhat(thTenDuong);
                        }
                    } break;
                default: ThoatXuLi(thTenDuong,_duLieu.MaDuong); break;
            }  
        }

        private void thTenPhuong_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            switch (e.Button.Index)
            {
                case 0:
                    {
                        if (thTenPhuong.Properties.Buttons[0].Tag != null)
                        {
                            DTOPhuong dto = new DTOPhuong();
                            dto.TenPhuong = thTenPhuong.Text + "";
                            BUSPhuong.ThemPhuong(dto);
                            SESSION._dsPhuong.Add(dto);
                            ThoatXuLi(thTenPhuong,dto.MaPhuong);
                        }
                        else
                        {
                            HienThiThemMoi(thTenPhuong);
                            thTenPhuong.Properties.Buttons[0].Tag = 1;
                        }
                    } break;
                case 1:
                    {
                        if (thTenPhuong.Properties.Buttons[1].Tag != null)
                        {
                            DTOPhuong dto = (DTOPhuong)thTenPhuong.Properties.Buttons[1].Tag;
                            dto.TenPhuong = thTenPhuong.Text;
                            BUSPhuong.CapNhatPhuong(dto);
                            ThoatXuLi(thTenPhuong,dto.MaPhuong);
                        }
                        else
                        {
                            thTenPhuong.Properties.Buttons[1].Tag = thTenPhuong.GetSelectedDataRow();
                            HienThiCapNhat(thTenPhuong);
                        }
                    } break;
                default: ThoatXuLi(thTenPhuong,_duLieu.MaPhuong); break;
            }  
        }

        private void thTenQuanHuyen_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            switch (e.Button.Index)
            {
                case 0:
                    {
                        if (thTenQuanHuyen.Properties.Buttons[0].Tag != null)
                        {
                            DTOQuanHuyen dto = new DTOQuanHuyen();
                            dto.TenQuanHuyen = thTenQuanHuyen.Text + "";
                            BUSQuanHuyen.ThemQuanHuyen(dto);
                            SESSION._dsQuanHuyen.Add(dto);
                            ThoatXuLi(thTenQuanHuyen,dto.MaQuanHuyen);
                        }
                        else
                        {
                            HienThiThemMoi(thTenQuanHuyen);
                            thTenQuanHuyen.Properties.Buttons[0].Tag = 1;
                        }
                    } break;
                case 1:
                    {
                        if (thTenQuanHuyen.Properties.Buttons[1].Tag != null)
                        {
                            DTOQuanHuyen dto = (DTOQuanHuyen)thTenQuanHuyen.Properties.Buttons[1].Tag;
                            dto.TenQuanHuyen = thTenQuanHuyen.Text;
                            BUSQuanHuyen.CapNhatQuanHuyen(dto);
                            ThoatXuLi(thTenQuanHuyen,dto.MaQuanHuyen);
                        }
                        else
                        {
                            thTenQuanHuyen.Properties.Buttons[1].Tag = thTenQuanHuyen.GetSelectedDataRow();
                            HienThiCapNhat(thTenQuanHuyen);
                        }
                    } break;
                default: ThoatXuLi(thTenQuanHuyen,_duLieu.MaQuanHuyen); break;
            }  
        }

        private void thTenTinhThanh_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            switch (e.Button.Index)
            {
                case 0:
                    {
                        if (thTenTinhThanh.Properties.Buttons[0].Tag != null)
                        {
                            DTOTinhThanh dto = new DTOTinhThanh();
                            dto.TenTinhThanh = thTenTinhThanh.Text + "";
                            BUSTinhThanh.ThemTinhThanh(dto);
                            SESSION._dsTinhThanh.Add(dto);
                            ThoatXuLi(thTenTinhThanh,dto.MaTinhThanh);
                        }
                        else
                        {
                            HienThiThemMoi(thTenTinhThanh);
                            thTenTinhThanh.Properties.Buttons[0].Tag = 1;
                        }
                    } break;
                case 1:
                    {
                        if (thTenTinhThanh.Properties.Buttons[1].Tag != null)
                        {
                            DTOTinhThanh dto = (DTOTinhThanh)thTenTinhThanh.Properties.Buttons[1].Tag;
                            dto.TenTinhThanh = thTenTinhThanh.Text;
                            BUSTinhThanh.CapNhatTinhThanh(dto);
                            ThoatXuLi(thTenTinhThanh,dto.MaTinhThanh);
                        }
                        else
                        {
                            thTenTinhThanh.Properties.Buttons[1].Tag = thTenTinhThanh.GetSelectedDataRow();
                            HienThiCapNhat(thTenTinhThanh);
                        }
                    } break;
                default: ThoatXuLi(thTenTinhThanh,_duLieu.MaTinhThanh); break;
            }  
        }                        
    }
}
